using System;
using System.Collections.Generic;
using System.Text;
using Ghasam.Business.Domain.Entity;
using NHibernate;
using NHibernate.Expression;

namespace Ghasam.Business.DAO.Impl
{
    public class EmployeeLeaveDao : DaoBase<EmployeeLeave>, IEmployeeLeaveDao
    {
        #region IEmployeeLeaveDao Members

        public int CountEmployeeLeaves(Employee employee, DateTime startDate, DateTime endDate) {
            ICriteria criteria = base.Session.CreateCriteria(typeof(EmployeeLeave));

            criteria.Add(Expression.Ge("FromDate", startDate));
            criteria.Add(Expression.Le("FromDate", endDate.AddDays(1)));

            if ( employee != null )
                criteria.Add(Expression.Eq("Employee", employee));

            criteria.SetProjection(Projections.Count("Id"));

            return criteria.UniqueResult<int>();
        }

        public IList<EmployeeLeave> FindEmployeeLeaves(Employee employee, DateTime startDate, DateTime endDate, int pageSize, int pageNumber) {
            ICriteria criteria = base.Session.CreateCriteria(typeof(EmployeeLeave));

            criteria.Add(Expression.Ge("FromDate", startDate));
            criteria.Add(Expression.Le("FromDate", endDate.AddDays(1)));

            if (employee != null)
                criteria.Add(Expression.Eq("Employee", employee));

            if (pageSize != -1)
                criteria.SetMaxResults(pageSize).SetFirstResult(pageSize * pageNumber);

            return criteria.List<EmployeeLeave>();
        }

        #endregion
    }
}
